Ext.define('framenet.view.mainTree', {
	extend: 'Ext.tree.Panel',
    requires: ['framenet.view.tabPanel'],
	alias: 'widget.maintree',

	initComponent: function() {
		var me = this;
		Ext.applyIf(me, {
			viewConfig: {}
		});
		me.callParent(arguments);
	},

    constructor: function(cfg) {
        var me = this;
        cfg = cfg || {};
        
        var treeStore = Ext.create('framenet.store.treeStore');
        Ext.Ajax.request({
		        method: 'GET',
		        url: 'getMainTreeItems.htm',
		        success: function(result, request)	{
		        	var responseObj = Ext.JSON.decode(result.responseText);
		        	treeStore.setRootNode({
		                text: 'System',
		                id: 'root',
		                expanded: true,
		                children: responseObj.items
		            });
		        }
		    });
        
        me.callParent([Ext.apply({
        	store: treeStore, 
    		title: 'Main Menu',
    		collapsible: true,
    		singleExpand: true,
    		width: 200
        }, cfg)]);
    },

	listeners:{
		itemclick: function(dataview, record, item, index, e, options){
			
			if (index == 0) return; // if root root return
			
			var clsNode = record.raw.cls;
			if (clsNode && clsNode == 'folder')	return; // if folder folder return
			
			var textItemMenu = record.raw.text;
			var entity = record.raw.entity;
			var jsFrameReport = record.raw.jsFrameReport;
			var jsFrameGrid = record.raw.jsFrameGrid;
			var jsFrameDetail = record.raw.jsFrameDetail;
			var jsFrameDataModel = record.raw.jsFrameDataModel;
			var jsFrameGridColModel = record.raw.jsFrameGridColModel;			
			var fieldsDataModelStr = record.raw.fieldsDataModel;
			var tabPanel =  Ext.getCmp('maintabPanel');
		    var tabFramePanel = Ext.getCmp(textItemMenu);

		    if (jsFrameReport)	{
			    Ext.create(jsFrameReport, {height: 160, width: 300});                    	        		   
		    }else	{
		    	if(!tabFramePanel)	{
			    	// set gridStoreModel
			    	var gridDataModel;
			    	if (!jsFrameDataModel)	{
				    	// define the generic model from dynamic fields
				    	Ext.define('GenericDataModel', {
				    		extend: 'Ext.data.Model',
				    		fields: Ext.decode(fieldsDataModelStr)
				    	});
				    	gridDataModel = GenericDataModel;
			    	}else	{
			    		gridDataModel = jsFrameDataModel;
			    	}
			    	
			    	// set generic framegrid if does not exists
			    	if (!jsFrameGrid)	{
			    		jsFrameGrid = 'framenet.view.frameGrid';
			    	}
			    	
			    	// create a frame panel
			    	tabFramePanel = Ext.create('framenet.view.framePanel', {title: textItemMenu});
			    	
			    	// create a store
			    	var gridStore = Ext.create('framenet.store.gridStore', {model: gridDataModel, entity: entity});

			    	// create panels grid and detail and set references
			    	var frameGrid = Ext.create(jsFrameGrid, {store: gridStore, columnModel: jsFrameGridColModel, framePanel: tabFramePanel, showSearcher: true});
			        var frameDetail = Ext.create(jsFrameDetail, {store: gridStore, framePanel: tabFramePanel});
			        frameDetail.hide();
			    	tabFramePanel.frameGrid = frameGrid;
			    	tabFramePanel.frameDetail = frameDetail;
			    	
			    	// add panels to tab page
			    	tabFramePanel.add(frameGrid);
			    	tabFramePanel.add(frameDetail);
		        
			    	// add tab page to tab panel
			    	tabPanel.add(tabFramePanel);  
			        tabPanel.doLayout();  
			    }  
			  
			    tabPanel.setActiveTab(tabFramePanel);
		    }
		}
	},
	
	getNodeValue: function(node) {
		var value = null;
    	if (node)	{
    		value = node.childNodes[0].nodeValue;
    	}
    	return value;
	}	
});
